//
//  AppDelegate.m
//  YL8G
//
//  Created by Liccon Chang on 12-7-13.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = window_;
@synthesize bkViewController = bkViewController_;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.window.backgroundColor=[UIColor whiteColor];
    engine_=[[MainEngine alloc] initWithWindow:window_];
    
    //加判断
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString* closeBaoku = [defaults objectForKey:@"closeBaoku"];
    if (closeBaoku ==nil || [closeBaoku isEqualToString:@"0"])
    {
        // 添加宝库的代码
        bkViewController_ = [[BaokuViewController alloc] init];
        bkViewController_.delegate = self;
        [bkViewController_ registerPushNotificationAndLaunchingWithOptions:launchOptions andAppItunesId:@"553118755"];
        // 添加提醒用户评论支持（默认应用第3次被激活弹出），URL要设置成你的应用在AppStore中的URL，一般只替换ID就行了。
        [bkViewController_ setCommentNoticeEnable:YES commentURL:[NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d", 553118755]]];
    }
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
    
    // 添加此行代码，可以使宝库自动弹出，默认为应用第5次被激活时弹出，以后每2次弹出。
    // 应用的激活次数靠此方法统计
    [bkViewController_ applicationDidBecomeActive];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    /*
     Called when the application is about to terminate.
     Save data if appropriate.
     See also applicationDidEnterBackground:.
     */
}

#pragma mark - 远程消息

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    // 注册push成功
    [bkViewController_ registerPushNotificationSuccessWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // 收到push消息
    [bkViewController_ applicationRecievePushNotification:userInfo];
}

#pragma mark - BaokuViewControllerDelegate

- (id)viewControllerForPresentModalView {
    // 返回window的rootViewController
    return engine_.mainViewController;
}

-(void)dealloc
{
	[bkViewController_ release];
	[super dealloc];
}
@end
